package com.buer.storage.service.impl;

import com.buer.storage.dao.DestroyDao;
import com.buer.storage.dto.*;
import com.buer.storage.entity.Dorder;
import com.buer.storage.entity.Stock;
import com.buer.storage.entity.Worder;
import com.buer.storage.service.DestroyService;
import com.buer.storage.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DestroyServiceImpl implements DestroyService {
    private SnowFlake snowFlake=new SnowFlake(2,3);
    @Resource
    private DestroyDao destroyDao;

    @Override
    public List<Stock> findStock() {
        return destroyDao.findStock();
    }

    @Override
    public PageInfo<Dorder> selectByAll(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<Dorder> list = destroyDao.selectByAll(map); // 获取数据
        PageInfo<Dorder> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Transactional
    @Override
    public int insert(DeStorageDto deStorageDto) {
        String orderNo=String.valueOf(snowFlake.nextId());
        deStorageDto.setDestroyNum("DS" + orderNo);

        int result = destroyDao.insertOrder(deStorageDto);
        for (int i = 0; i < deStorageDto.getDestroyProds().length; i++) {
            DestroydetailsDto outProd = deStorageDto.getDestroyProds()[i];
            outProd.setDestroyId(deStorageDto.getDestroyId());
//            Integer prodNum = outProd.getProdNum();
//
//            Stock stock = destroyDao.queryBuerStockByParam(outProd);
//
//            Integer u = prodNum + stock.getProdAwaitnum();
//            int q = stock.getProdStocknum();
//            Integer w = q - u;
//            stock.setProdAwaitnum(u);
//            stock.setProdResiduenum(w);
//            destroyDao.updateAwaitNum(stock);
//            System.out.println(stock);

        }

        destroyDao.insertDetails(deStorageDto.getDestroyProds());

        return result;
    }

    @Override
    public PageInfo<DestroyDto> findDestroyAll(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<DestroyDto> list = destroyDao.findDestroyAll(map); // 获取数据
        PageInfo<DestroyDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Transactional
    @Override
    public int deleteDestroyById(int destroyId) {
        int deletedorderById = destroyDao.deleteOutStorageById(destroyId);
        return deletedorderById;
    }

    @Override
    public int deleteDestroydetails(int detailsId) {
        int deletedorderdetails = destroyDao.deleteDorderdetails(detailsId);
        return deletedorderdetails;
    }

    @Override
    public List<DeStorageAllDto> findOutAll(int destroyId) {
        List<DeStorageAllDto> all = destroyDao.findOutAll(destroyId);
        if (all.size() > 0) {
            for (int i = 0; i < all.size(); i++) {
                DestroydetailsDto[] info = destroyDao.findOrderDetailInfoByOutId(all.get(i).getDestroyId());
                if (info.length > 0) {
                    all.get(i).setDestroyProds(info);
                }
            }
        }
        return all;
    }

    @Override
    @Transactional
    public int updateDestroydetails(DeStorageDto deStorageDto) {
        Integer destroyId = deStorageDto.getDestroyId();
        destroyDao.deleteDorderByOutId(destroyId);
        destroyDao.deleteDorderDetailsByOutId(destroyId);
        // 第二部再插入
        //outStorageDto.setOutStatus(1);
        int result = destroyDao.insertOrder(deStorageDto);
        for (int i = 0; i < deStorageDto.getDestroyProds().length; i++) {
            DestroydetailsDto outProd = deStorageDto.getDestroyProds()[i];
            outProd.setDestroyId(deStorageDto.getDestroyId());
        }
        destroyDao.insertDetails(deStorageDto.getDestroyProds());
        return result;
    }

    @Override
    @Transactional
    public int submitAddBuerStock(int destroyId) {

        DestroydetailsDto[] outProds = destroyDao.findDordertailsAll(destroyId);

        int result = 0;
        for (int i = 0; i < outProds.length; i++) {
            result = 1;
            DestroydetailsDto outProd = outProds[i];
            //先查找prodArea,prodDepart,prodId
            Stock stock = destroyDao.queryBuerStockByParam(outProd);

            //更新累加
            Integer prodNum = outProd.getProdNum();
            Integer newProdNum = stock.getProdStocknum() - prodNum;

            Integer stockId = stock.getStockId();
            stock.setProdStocknum(newProdNum);

//            Integer prodAwaitnum = stock.getProdAwaitnum();
//            int o = prodAwaitnum - prodNum;
//            int q = stock.getProdStocknum();
//
//            Integer w = q - o;
//            stock.setProdAwaitnum(o);
//            stock.setProdResiduenum(w);
//            destroyDao.updateAwaitNum(stock);
            if (newProdNum == 0) {
                destroyDao.deleteStock(stockId);

            } else {
                destroyDao.updateAddSumStock(stock);
                destroyDao.updateStatus(destroyId);

            }
//                Integer stockdetailsId = outProd.getStockdetailsId();
//                destroyDao.deleteStockDetails(stockdetailsId);

        }

        return result;
    }
}
